package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CusterException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-06-06
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 保存
     * @param dto
     */
    @Transactional
    public void saveBysetmealDto(SetmealDto dto) {
        this.save(dto);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);



    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    public SetmealDto getSetmealByid(Long id) {
        SetmealDto setmealDto =new SetmealDto();
        Setmeal byId = this.getById(id);
        BeanUtils.copyProperties(byId,setmealDto);
        LambdaQueryWrapper<SetmealDish> qw =new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(qw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 更新
     * @param dto
     */

    public void updateBysetmealDto(SetmealDto dto) {
        this.updateById(dto);
        //删除之前的数据
        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,dto.getId());
        setmealDishService.remove(qw);
        //添加数据
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);


    }

    /**
     * 通过id删除或批量删除
     * @param ids
     */
       @Transactional
        public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw1 =new LambdaQueryWrapper<>();
        qw1.in(Setmeal::getId,ids);
        qw1.eq(Setmeal::getStatus,1);
        int count = setmealService.count(qw1);
        if (count>0){
            throw new CusterException("无法删除");
        }
        setmealService.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> qw =new LambdaQueryWrapper<>();
        qw.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(qw);
    }


    public Page selectBypage(int page, int pageSize, String name) {
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper();
        //添加过滤条件
        qw.like( Setmeal::getName, name);
        //添加排序条件
        qw.orderByAsc(Setmeal::getUpdateTime);
        //调用方法
        setmealService.page(pageInfo);
        //数据拷贝
//        BeanUtils.copyProperties(pageInfo,dish);
        //查询出来的套餐基本信息
        List<Setmeal> records = pageInfo.getRecords();
        //1)查询所有分类信息
        Set<Long> collect = records.stream().map(setmeal -> (setmeal.getCategoryId())).collect(Collectors.toSet());

        List<Category> category = categoryService.listByIds(collect);
        //2)获取 categoryId  和  categoryName 的对应关系
        Map<Long, String> collect1 = category.stream().collect(Collectors.toMap(Category::getId, Category::getName));

        for (Setmeal record : records) {
            Long categoryId = record.getCategoryId();
            String dishName = collect1.get(categoryId);
            record.setCategoryName(dishName);

        }
        //4)返回数据
        pageInfo.setRecords(records);
        return pageInfo;
    }
}
